package com.zhanghu.backendZray.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhanghu.backendZray.constants.CommonConstant;
import com.zhanghu.backendZray.converter.RoleConverter;
import com.zhanghu.backendZray.mapper.RoleMapper;
import com.zhanghu.backendZray.pojo.entity.Role;
import com.zhanghu.backendZray.pojo.request.role.RolePageRequest;
import com.zhanghu.backendZray.pojo.request.role.RoleRequest;
import com.zhanghu.backendZray.pojo.vo.RoleVo;
import com.zhanghu.backendZray.service.RoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhangdong
 * @since 2024-07-20
 */
@Service
@RequiredArgsConstructor
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    private final RoleConverter roleConverter;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(RoleRequest roleRequest) {
        Role role = roleConverter.input2Entity(roleRequest);
        role.setId(null);
        baseMapper.insert(role);
    }

    @Override
    public void update(RoleRequest request) {
        Role role = roleConverter.input2Entity(request);
        baseMapper.updateById(role);
    }


    @Override
    public Page<RoleVo> page(RolePageRequest request) {
        Page<Role> pageQuery = new Page<>(request.getPageNum(), request.getPageSize());
        Page<Role> page = baseMapper.selectPage(pageQuery, Wrappers.lambdaQuery(new Role())
                .like(StrUtil.isNotEmpty(request.getName()), Role::getName, request.getName())
                .like(StrUtil.isNotEmpty(request.getEnName()), Role::getEnName, request.getEnName())
        );
        List<Role> list = page.getRecords();
        List<RoleVo> roleVos = roleConverter.entity2Vos(list);
        Page<RoleVo> resultPage = new Page<>(page.getCurrent(), page.getSize(), page.getTotal());
        resultPage.setRecords(roleVos);
        return resultPage;
    }

    /**
     * 获取游客角色
     */
    @Override
    public Integer getGuestRoleId() {
        Role role = baseMapper.selectOne(new LambdaQueryWrapper<Role>().eq(Role::getEnName, CommonConstant.GUEST_ROLE));
        if (role == null) {
            return null;
        }
        return role.getId();
    }

    /**
     * 获取试用角色
     */
    @Override
    public Integer getTrialRoleId() {
        Role role = baseMapper.selectOne(new LambdaQueryWrapper<Role>().eq(Role::getEnName, CommonConstant.TRIAL_ROLE));
        if (role == null) {
            return null;
        }
        return role.getId();
    }

    /**
     * 获取会员角色
     */
    @Override
    public Integer getMemberRoleId() {
        Role role = baseMapper.selectOne(new LambdaQueryWrapper<Role>().eq(Role::getEnName, CommonConstant.MEMBER_ROLE));
        if (role == null) {
            return null;
        }
        return role.getId();
    }

    /**
     * 获取家庭组角色
     */
    @Override
    public Integer getFamilyRoleId() {
        Role role = baseMapper.selectOne(new LambdaQueryWrapper<Role>().eq(Role::getEnName, CommonConstant.FAMILY_GROUP_ROLE));
        if (role == null) {
            return null;
        }
        return role.getId();
    }

}
